package com.zwr.sheller.shop.listener;

import com.zwr.sheller.core.common.LSYBusinessException;
import com.zwr.sheller.core.pojo.RefundImg;
import com.zwr.sheller.core.pojo.RefundVo;
import com.zwr.sheller.core.util.JsonUtil;
import com.zwr.sheller.core.util.OSSUtil;
import com.zwr.sheller.shop.mapper.RefundImgMapper;
import com.zwr.sheller.shop.mapper.RefundMapper;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author LSY
 * @Date 2022/1/13 11:37
 * @Version 1.0
 **/
@Component
@RabbitListener(queues = "refundQueue")
public class CustomerRefundListener {

    RefundMapper refundMapper;
    RefundImgMapper refundImgMapper;
    @Autowired(required = false)
    public CustomerRefundListener(RefundMapper refundMapper, RefundImgMapper refundImgMapper) {
        this.refundMapper = refundMapper;
        this.refundImgMapper = refundImgMapper;
    }

    @RabbitHandler
    @Transactional(rollbackFor = Throwable.class)
    public void handler(String json) throws LSYBusinessException {
        RefundVo refundVo = JsonUtil.stringToObject(json, RefundVo.class);

        refundMapper.insertSelective(refundVo.getRefund());
        for (RefundImg refundImg : refundVo.getRefundImgs()) {
            refundImgMapper.insertSelective(refundImg);
        }

        for (RefundImg refundImg : refundVo.getRefundImgs()) {
            OSSUtil.moveFile(refundImg.getRefundImgPath().substring(27), "refund" + refundImg.getRefundImgPath().substring(36));
        }
    }
}
